{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {
    "id": "2e59330c",
    "language": "markdown"
   },
   "source": [
    "# DuckDuckGo Search Tool\n",
    "\n",
    "The DuckDuckGo search integration allows users to perform real-time web searches within the AG2 framework using the DuckDuckGo search engine. Follow these steps to integrate `DuckDuckGoSearchTool` with AG2 Agents.\n",
    "\n",
    "## Configuration\n",
    "\n",
    "DuckDuckGo Search does **not** require an API key, making it simple to set up."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {
    "id": "7426ee2f",
    "language": "markdown"
   },
   "source": [
    "\n",
    "## Package Installation\n",
    "\n",
    "To get started with the `DuckDuckGo Search` integration in AG2, you need to install the necessary package:\n",
    "\n",
    "Install AG2 with `openai` and `duckduckgo` extras:\n",
    "```bash\n",
    "pip install -U \"ag2[openai,duckduckgo]\"\n",
    "```\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:\n",
    "> ```bash\n",
    "> pip install -U \"autogen[openai,duckduckgo]\"\n",
    "> ```\n",
    "> or\n",
    "> ```bash\n",
    "> pip install -U \"ag2[openai,duckduckgo]\"\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.\n",
    "\n",
    "\n",
    "You're all set! Now you can start using DuckDuckGo Search with AG2."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {
    "id": "9faf6169",
    "language": "markdown"
   },
   "source": [
    "## Implementation\n",
    "The `DuckDuckGoSearchTool` enables agents to perform real time web searches."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {
    "id": "aaa2aec7",
    "language": "markdown"
   },
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {
    "id": "3922eb84",
    "language": "python"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from autogen import AssistantAgent, UserProxyAgent\n",
    "from autogen.tools.experimental import DuckDuckGoSearchTool"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {
    "id": "1a25ff3d",
    "language": "markdown"
   },
   "source": [
    "### Agent Configuration\n",
    "\n",
    "Configure an assistant agent and user proxy to be used for LLM recommendation and execution respectively.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {
    "id": "00ed58d3",
    "language": "python"
   },
   "outputs": [],
   "source": [
    "os.environ[\"AUTOGEN_USE_DOCKER\"] = \"False\"\n",
    "\n",
    "from autogen import LLMConfig\n",
    "\n",
    "# Configure your LLM settings. You can use OAI_CONFIG_LIST_sample as a template.\n",
    "config_list = LLMConfig.from_local(\"OAI_CONFIG_LIST\", api_type=\"openai\", model=\"gpt-5-nano\")\n",
    "\n",
    "assistant = AssistantAgent(\n",
    "    name=\"assistant\",\n",
    "    llm_config=config_list,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(name=\"user_proxy\", human_input_mode=\"NEVER\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {
    "id": "3153c8a8",
    "language": "markdown"
   },
   "source": [
    "### Tool Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {
    "id": "4b57d037",
    "language": "python"
   },
   "outputs": [],
   "source": [
    "# Initialize the DuckDuckGoSearchTool. No API key is needed.\n",
    "duckduckgo_search_tool = DuckDuckGoSearchTool()\n",
    "\n",
    "# Register the tool for LLM recommendation and execution.\n",
    "duckduckgo_search_tool.register_for_llm(assistant)\n",
    "duckduckgo_search_tool.register_for_execution(user_proxy)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {
    "id": "720125df",
    "language": "markdown"
   },
   "source": [
    "### Start the Conversation\n",
    "\n",
    "With the setup complete, you can now use the assistant to fetch live web search results using DuckDuckGo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {
    "id": "cc26e476",
    "language": "python"
   },
   "outputs": [],
   "source": [
    "response = user_proxy.initiate_chat(\n",
    "    recipient=assistant,\n",
    "    message=\"What happened with stock prices after deepseek was launched? Please search the web using DuckDuckGo.\",\n",
    "    max_turns=2,\n",
    ")\n",
    "print(f\"Final Answer: {response.summary}\")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "DuckDuckGo Search Tool",
   "tags": [
    "tools",
    "DuckDuckGo",
    "web-search",
    "search"
   ]
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
